package com.supermap.dataservice.monitor;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.monitor.dao.MonitorDao;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.monitor.MonitorService;
import com.supermap.dubbo.api.monitor.entity.Active;
import com.supermap.dubbo.api.monitor.entity.AgrCrop;
import com.supermap.dubbo.api.monitor.entity.SerObj;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author lxs
 * @Title:监控平台业务层
 * @Description:
 * @date 2018/7/11  11:23
 */
@Service
public class MonitorServiceimp implements MonitorService{
    /**
     *使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(MonitorServiceimp.class);
    @Resource
    private MonitorDao monitorDao;
    @Override
    public JSONUtil getAgrCropNumsByAreacode(String areacode) {
        //初始化Json封装工具类
        Map m=new HashMap();
        JSONUtil jsonUtil = new JSONUtil();
        try {
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);//获取当前年份
            if(areacode!=null){
                areacode=areacode+"%";
            }
            List<AgrCrop> getAgrCropNumsList = monitorDao.getAgrCropsByAreacode(areacode, year);
            List<String> typesList = monitorDao.getCropsTypesList();
            //重装数据 返回给前端
            int[] a;;
            for (int i = 0; i <typesList.size() ; i++) {
                a=new int[12];
                String tt=typesList.get(i);
                for (int j = 0; j < getAgrCropNumsList.size(); j++) {
                    int num=getAgrCropNumsList.get(j).getNums();
                    String mon = getAgrCropNumsList.get(j).getMon();
                    String type=getAgrCropNumsList.get(j).getType();
                    if(type.equals(tt)){
                        a[Integer.parseInt(mon)-1]=num;
                        m.put(tt,a);
                    }else{
                        m.put(tt,a);
                    }
                }
            }
            jsonUtil.setMsg("Get AgrCropNums success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(m);
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("MonitorServiceImpl=>MonitorService=>DaoException:"+e.getStackTrace());
            jsonUtil.setMsg("Get AgrCropNums flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil  getSerObjListByAreacode(String areaCode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        Map m=new HashMap();
        try {
            if(areaCode!=null){
                areaCode=areaCode+"%";
            }
            Integer a;
            List<SerObj> list = monitorDao.getSerObjListByAreacode(areaCode);
            List<SerObj> roleList = monitorDao.getRoleList();
            for (int i = 0; i <roleList.size() ; i++) {
                a=new Integer(0);
                String roleName = roleList.get(i).getName();
                for (int j = 0; j <list.size() ; j++) {
                    String serName = list.get(j).getName();
                    int nmms=list.get(j).getNums();
                    if(roleName.equals(serName)){
                        a=nmms;
                        m.put(roleName,a);
                    }else{
                        m.put(roleName,a);
                    }
                }
            }
            jsonUtil.setMsg("Get SerObjNums success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(m);
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("MonitorServiceImpl=>MonitorService=>DaoException:"+e.getStackTrace());
            jsonUtil.setMsg("Get SerObjNums flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getSerObjListByAreacodeAndYearMonth(String areaCode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);//获取当前年份
        int mon = cal.get(Calendar.MONTH)+1;//获取当前月份
        Map m=new HashMap();
        Integer a;
        try {
            if(areaCode !=null){
                areaCode=areaCode+"%";
            }
            List<SerObj> list = monitorDao.getSerObjListByAreacodeAndYearMonth(areaCode, year, mon);
            List<SerObj> roleList = monitorDao.getRoleList();
            for (int i = 0; i <roleList.size() ; i++) {
                a=new Integer(0);
                String roleName = roleList.get(i).getName();
                for (int j = 0; j <list.size() ; j++) {
                    String serName = list.get(j).getName();
                    int nmms=list.get(j).getNums();
                    if(roleName.equals(serName)){
                        a=nmms;
                        m.put(roleName,a);
                    }else{
                        m.put(roleName,a);
                    }
                }
            }
            jsonUtil.setMsg("Get getSerObjListByAreacodeAndYearMonth success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(m);
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("MonitorServiceImpl=>MonitorService=>DaoException:"+e.getStackTrace());
            jsonUtil.setMsg("Get getSerObjListByAreacodeAndYearMonth flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getActiveListByareaCode(String areaCode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            if(areaCode !=null){
                areaCode=areaCode+"%";
            }
            List<Active> list = monitorDao.getActiveListByareaCode(areaCode);
            List<Active> list2=new ArrayList<>();
            jsonUtil.setMsg("Get getSerObjListByAreacodeAndYearMonth success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            if(list.size()>10){
                for (int i = 0; i <10 ; i++) {
                    list2.add(list.get(i));
                }
                jsonUtil.setData(list2);
            }else{
                jsonUtil.setData(list);
            }
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("MonitorServiceImpl=>MonitorService=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get getActiveListByareaCode flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getActiveCommmentListByuserId(String userId) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<Active> list = monitorDao.getTreeList(userId);
            List<Active> list2=new ArrayList<>();
            jsonUtil.setMsg("Get getSerObjListByAreacodeAndYearMonth success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            if(list.size()>3){
                for (int i = 0; i <3 ; i++){
                    Integer pid = list.get(i).getParentId();
                    Active active = monitorDao.getPComment(String.valueOf(pid));
                    list.get(i).setActivePinfo(active);
                    list2.add(list.get(i));
                }
                jsonUtil.setData(list2);
            }else{
                for (Active a:list){
                    Integer pid = a.getParentId();
                    Active active = monitorDao.getPComment(String.valueOf(pid));
                    a.setActivePinfo(active);
                }
                jsonUtil.setData(list);
            }
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("MonitorServiceImpl=>MonitorService=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get getActiveCommmentListByareaid flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getAllList(String areaCode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);//获取当前年份
        Map m=new HashMap();
        //重装数据 返回给前端
        int[] alladv=new int[12];
        int[] allagr=new int[12];
        int[] alldis=new int[12];;
        try{
            if(areaCode !=null){
                areaCode=areaCode+"%";
            }
            //咨询
            List<AgrCrop> advList = monitorDao.getAdvList(year, areaCode);
            if(advList.size()>0){
                for(AgrCrop adv:advList){
                    String mon = adv.getMon();
                    int nums = adv.getNums();
                    alladv[Integer.parseInt(mon)-1]=nums;
                }
            }
            m.put("咨询",alladv);
            //农情
            List<AgrCrop> agrList = monitorDao.getAgrList(year, areaCode);
            if(agrList.size()>0){
                for(AgrCrop agr:agrList){
                    String mon = agr.getMon();
                    int nums = agr.getNums();
                    allagr[Integer.parseInt(mon)-1]=nums;
                }
            }
            m.put("农情",allagr);
            //灾情
            List<AgrCrop> disList = monitorDao.getDisList(year, areaCode);
            if(disList.size()>0){
                for(AgrCrop dis:disList){
                    String mon = dis.getMon();
                    int nums = dis.getNums();
                    alldis[Integer.parseInt(mon)-1]=nums;
                }
            }
            m.put("灾情",alldis);
            jsonUtil.setMsg("Get getAllList success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(m);
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("MonitorServiceImpl=>MonitorService=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get getAllList flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }
}
